
Imports System
Imports System.Data.SqlClient
Imports System.Diagnostics
Imports _2bMatesSite.Common


Namespace _2bMatesSite.BO
	''' <summary>
	''' Represents an instance of a blocked user.
	''' </summary>
	Public Class BlockedUser
		Protected ID As Integer
		Protected UID As Integer
		Protected blockedUID As Integer
		Protected reason As String
		Protected [date] As DateTime

		Public Sub New(ByVal dr As SqlDataReader)
			Init(dr)
		End Sub

		Public Sub New(ByVal usr As User, ByVal blockedUsr As User)
			Init(usr.ID, blockedUsr.ID, "")
		End Sub

		Public Sub New(ByVal usr As User, ByVal blockedUsr As User, ByVal reason As String)
			Init(usr.ID, blockedUsr.ID, reason)
		End Sub

		Public Sub New(ByVal UID As Integer, ByVal blockedUID As Integer)
			Init(UID, blockedUID, "")
		End Sub

		Public Sub New(ByVal UID As Integer, ByVal blockedUID As Integer, ByVal reason As String)
			Init(UID, blockedUID, reason)
		End Sub

		Public Overloads Overrides Function GetHashCode() As Integer
			Return blockedUID
		End Function

		Public ReadOnly Property UserID() As Integer
			Get
				Return UID
			End Get
		End Property

		Public ReadOnly Property BlockedUID() As Integer
			Get
				Return blockedUID
			End Get
		End Property

		Public ReadOnly Property BlockedUsr() As User
			Get
				Return Users.GetUserByID(blockedUID)
			End Get
		End Property

		Public ReadOnly Property Reason() As String
			Get
				Return reason
			End Get
		End Property

		Public ReadOnly Property [Date]() As DateTime
			Get
				Return [date]
			End Get
		End Property

		Public Sub Save()
			If ID < 0 Then
				DoInsert()
			Else
				DoUpdate()
			End If
		End Sub

		Public Sub Delete()
			DoDelete()
		End Sub

		Protected Sub DoInsert()
			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()

			Try
				Dim cmd As New SqlCommand(GetInsertSQL())
				cmd.Connection = conn

				PopulateCommand(cmd)

				Dim rows As Integer = cmd.ExecuteNonQuery()
				Debug.Assert(rows = 1)


				cmd = New SqlCommand("SELECT * FROM BlockedUsers WHERE UID=@UID AND BlockedUID=@BlockedUID")
				cmd.Connection = conn
				cmd.Parameters.Add("@UID", UID)
				cmd.Parameters.Add("@BlockedUID", blockedUID)
				Dim dr As SqlDataReader = cmd.ExecuteReader()

				dr.Read()
				Init(dr)
				dr.Close()
			Finally
				conn.Close()
			End Try
		End Sub

		Protected Sub DoUpdate()
			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()

			Try
				Dim cmd As New SqlCommand(GetUpdateSQL())
				cmd.Connection = conn

				PopulateCommand(cmd)
				cmd.Parameters.Add("@ID", ID)

				Dim rows As Integer = cmd.ExecuteNonQuery()
				Debug.Assert(rows = 1)
			Finally
				conn.Close()
			End Try
		End Sub

		Protected Sub DoDelete()
			Dim conn As SqlConnection = DBManager.GetUnmanagedConnection()

			Try
				Dim cmd As New SqlCommand("DELETE FROM BlockedUsers WHERE UID=@UID AND BlockedUID=@BlockedUID")
				cmd.Connection = conn

				cmd.Parameters.Add("@UID", UID)
				cmd.Parameters.Add("@BlockedUID", blockedUID)

				Dim rows As Integer = cmd.ExecuteNonQuery()
				Debug.Assert(rows = 1)
			Finally
				conn.Close()
			End Try
		End Sub

		Protected Function GetInsertSQL() As String
			Dim sql As String = "INSERT INTO BlockedUsers (UID, BlockedUID, Reason)" + Environment.NewLine
			sql += "VALUES (@UID, @BlockedUID, @Reason)" + Environment.NewLine
			Return sql
		End Function

		Protected Function GetUpdateSQL() As String
			Dim sql As String = "UPDATE BlockedUsers" + Environment.NewLine
			sql += "SET" + Environment.NewLine
			sql += "" & Chr(9) & "UID = @UID," + Environment.NewLine
			sql += "" & Chr(9) & "BlockedUID = @BlockedUID," + Environment.NewLine
			sql += "" & Chr(9) & "Reason = @Reason" + Environment.NewLine
			sql += "WHERE" + Environment.NewLine
			sql += "" & Chr(9) & "ID = @ID" + Environment.NewLine
			Return sql
		End Function

		Protected Sub PopulateCommand(ByVal cmd As SqlCommand)
			cmd.Parameters.Add("@UID", UID)
			cmd.Parameters.Add("@BlockedUID", blockedUID)
			DBManager.SetNZValue(cmd, "@Reason", reason)
		End Sub

		Protected Sub Init(ByVal dr As SqlDataReader)
			Dim ID As Integer = dr.GetInt32(dr.GetOrdinal("ID"))
			Dim UID As Integer = dr.GetInt32(dr.GetOrdinal("UID"))
			Dim BlockedUID As Integer = dr.GetInt32(dr.GetOrdinal("BlockedUID"))
			Dim Reason As String = DBManager.GetNZString(dr, "Reason")
			Dim [Date] As DateTime = dr.GetDateTime(dr.GetOrdinal("Date"))

			Init(ID, UID, BlockedUID, Reason, [Date])
		End Sub

		Protected Sub Init(ByVal ID As Integer, ByVal UID As Integer, ByVal blockedUID As Integer, ByVal reason As String, ByVal [date] As DateTime)
			Init(UID, blockedUID, reason)
			Me.ID = ID
			Me.[date] = [date]
		End Sub

		Protected Sub Init(ByVal UID As Integer, ByVal blockedUID As Integer, ByVal reason As String)
			Me.ID = -1
			Me.[date] = New DateTime(0)

			Me.UID = UID
			Me.blockedUID = blockedUID
			Me.reason = reason
		End Sub
	End Class
End Namespace






